package com.sailors.common.task.provider.infra.design.common.rest;

import com.alibaba.fastjson.JSON;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.List;


@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
@Getter
@NoArgsConstructor
public class BasePageResponse<T> extends Response implements Serializable {
    private static final long serialVersionUID = -2815777825039155879L;
    private final static Logger LOGGER = LoggerFactory.getLogger(BasePageResponse.class);
    protected Long pageSize;
    protected Long pageNumber;
    protected Long total = 0L;
    protected Long pageCount = 0L;
    protected List<T> records;

    public BasePageResponse(Response response) {
        super(response.getReturnCode(), response.getMessage(), response.getChainId());
    }

    public BasePageResponse(int code, String message) {
        super(code, message, null);
    }

    public BasePageResponse<T> build(Long pageSize, Long pageNumber, Long totalRecord, List<T> records) {
        this.pageSize = pageSize;
        this.pageNumber = pageNumber;
        this.total = totalRecord;
        this.records = records;
        this.pageCount = totalRecord % pageSize == 0 ? totalRecord / pageSize : totalRecord / pageSize + 1;
        return this;
    }

    public BasePageResponse buildA(Long pageNumber, Long pageSize, Long totalRecord, List<T> records, Long pageCount) {
        LOGGER.info("buildA 值分别为：{}，{}，{}，{},{}", pageNumber, pageSize, totalRecord, JSON.toJSONString(records), pageCount);
        this.pageSize = pageSize;
        this.pageNumber = pageNumber;
        this.total = totalRecord;
        this.records = records;
        this.pageCount = pageCount;
        return this;
    }
}
